package com.yh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.common.BaseResult;
import com.yh.common.Warning;
import com.yh.entity.SysUser;
import com.yh.entity.vo.LoginUserVO;
import com.yh.exception.BaseException;
import com.yh.mapper.SysRoleMapper;
import com.yh.mapper.SysUserMapper;
import com.yh.security.JwtUtil;
import com.yh.security.MyUserDetailsService;
import com.yh.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 用户登录信息表 服务实现类
 * </p>
 *
 * @author yh
 * @since 2021-04-06
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private final SysUserMapper sysUserMapper;
    private final MyUserDetailsService myUserDetailsService;
    private final JwtUtil jwtUtil;
    private final SysRoleMapper sysRoleMapper;

    @Override
    public SysUser createUser(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @Override
    public SysUser updateUser(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        return sysUser;
    }

    @Override
    public SysUser getInfo(Long id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public Page<SysUser> getPage(Integer pageNum, Integer pageSize) {
        return sysUserMapper.selectPage(new Page<>(pageNum,pageSize),new LambdaQueryWrapper<>());
    }

    @Override
    public LoginUserVO login(SysUser sysUser) {
        UserDetails userDetails = myUserDetailsService.loadUserByUsername(sysUser.getUsername());
        if(StringUtils.isEmpty(userDetails)){
           throw new BaseException(Warning.ERRNAME_PWD.getValue(),Warning.ERRNAME_PWD.getName());
        }
        if(!userDetails.getPassword().equals(sysUser.getPassword())){
            throw new BaseException(Warning.ERRNAME_PWD.getValue(),Warning.ERRNAME_PWD.getName());
        }
        SysUser user = sysUserMapper.getUser(sysUser.getUsername());
        LoginUserVO loginUserVO = new LoginUserVO();
        loginUserVO.setToken(jwtUtil.sign(user.getUsername()));
        loginUserVO.setPermission(userDetails.getAuthorities());
        loginUserVO.setSysUser(user);
        return loginUserVO;
    }

    @Override
    public Map<String,Object> getUserInfo(String token) {
        String username = jwtUtil.verity(token);
        SysUser user = sysUserMapper.getUser(username);
        Map<String,Object> map = new HashMap<>(16);
        map.put("roles",sysRoleMapper.roles(user.getId()));
        map.put("name",user.getUsername());
        map.put("avatar",user.getAvatar());
        map.put("introduction",user.getNickName());
        return map;
    }

}
